package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.easyViewHolder.CommodityDetailsEvaluationItemHolder;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.CommodityDetailsBean;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lihao on 2018/1/8.
 */

public class CommodityDetailsEvaluationListAdapter extends RecyclerArrayAdapter<CommodityDetailsBean.DataBean.CommentBean> {

    private ReplyInterface replyInterface;

    public void setReplyInterface(ReplyInterface replyInterface) {
        this.replyInterface = replyInterface;
    }

    public CommodityDetailsEvaluationListAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new FindDetailsEvaluationListHolder(parent);
    }

    class FindDetailsEvaluationListHolder extends BaseViewHolder<CommodityDetailsBean.DataBean.CommentBean> {

        @Bind(R.id.tv_user_name)
        TextView tvUserName;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.m_recyclerView)
        RecyclerView mRecyclerView;
        @Bind(R.id.img_head)
        ImageView imgHead;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_like)
        TextView tvLike;
        @Bind(R.id.tv_reply)
        TextView tvReply;
        @Bind(R.id.sub_layout)
        LinearLayout subLayout;
        @Bind(R.id.tv_more)
        TextView tvMore;
        @Bind(R.id.img_like)
        ImageView imgLike;
        @Bind(R.id.tv_status)
        TextView tvStatus;
        @Bind(R.id.tv_attr)
        TextView tvAttr;

        public FindDetailsEvaluationListHolder(ViewGroup parent) {
            super(parent, R.layout.item_product_details_evaluation_list);
            ButterKnife.bind(this, itemView);

            LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.context);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            mRecyclerView.setLayoutManager(layoutManager);
            mRecyclerView.setNestedScrollingEnabled(false);
            mRecyclerView.setAdapter(mAdapter);
        }

        RecyclerArrayAdapter mAdapter = new RecyclerArrayAdapter(BaseActivity.context) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new CommodityDetailsEvaluationItemHolder(parent);
            }
        };

        @Override
        public void setData(CommodityDetailsBean.DataBean.CommentBean data) {
            super.setData(data);
            Glide.with(BaseActivity.context)
                    .load(data.portrait)
                    .apply(MyApplication.headOptions)
                    .into(imgHead);
            tvUserName.setText(data.nickname);
            tvTime.setText(data.addtime);
            tvLike.setText(data.likes + "");
            tvContent.setText(data.content);


            if (data.buy_status==1){
                tvStatus.setText("未购买");
                tvStatus.setTextColor(BaseActivity.context.getResources().getColor(R.color.text_color_gray));
                tvStatus.setBackgroundResource(R.drawable.rb_unselect_bg);
                tvAttr.setVisibility(View.GONE);
            }else {
                tvStatus.setText("已购买");
                tvStatus.setTextColor(BaseActivity.context.getResources().getColor(R.color.theme_color));
                tvStatus.setBackgroundResource(R.drawable.theme_frame);
                if (TextUtils.isEmpty(data.product_option)){
                    tvAttr.setText("标准");
                }else {

                    tvAttr.setText(data.product_option);
                }
                tvAttr.setVisibility(View.VISIBLE);
            }

            if (data.is_liked == 2) {
                imgLike.setImageResource(R.drawable.ic_already_like);
            } else {
                imgLike.setImageResource(R.drawable.ic_not_like);
            }

            if (data.sub != null && data.sub.size() > 0) {
                mAdapter.clear();
                mAdapter.addAll(data.sub);
                subLayout.setVisibility(View.VISIBLE);
                if (data.more == 1) {
                    tvMore.setVisibility(View.GONE);
                } else {
                    tvMore.setVisibility(View.VISIBLE);
                }
            } else {
                subLayout.setVisibility(View.GONE);
            }
            tvReply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (replyInterface != null) {
                        replyInterface.reply(getDataPosition());
                    }
                }
            });
            tvLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (replyInterface != null) {
                        replyInterface.likes(getDataPosition());
                    }
                }
            });
            imgLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (replyInterface != null) {
                        replyInterface.likes(getDataPosition());
                    }
                }
            });
            tvMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (replyInterface != null) {
                        replyInterface.more(getDataPosition());
                    }
                }
            });


        }
    }

    public interface ReplyInterface {
        void reply(int position);

        void likes(int position);

        void more(int position);
    }
}
